import {
  CART_LIST,
  CHECK_ALL,
  UN_CHECK_ALL,
  CHECK_ITEM,
} from "./mutations-types";
import { Toast, Dialog } from "vant";
import http from "@/common/api/request";
export default {
  state: {
    list: [], //购物车数据
    selectList: [], //选中数据
  },
  getters: {
    isCheckedAll(state) {
      return state.list.length == state.selectList.length;
    },
    total(state) {
      let total = {
        num: 0,
        price: 0,
      };
      state.list.forEach((v) => {
        if (v.checked) {
          total.num += parseInt(v.goods_num);
          total.price += v.goods_price * v.goods_num;
        }
      });
      return total;
    },
  },
  mutations: {
    [CART_LIST](state, cartArr) {
      state.list = cartArr;
      state.selectList = state.list.map((v) => {
        return v.id;
      });
    },
    //全选
    [CHECK_ALL](state) {
      state.selectList = state.list.map((v) => {
        v.checked = true;
        return v.id;
      });
    },
    //全不选
    [UN_CHECK_ALL](state) {
      state.list.forEach((v) => {
        v.checked = false;
      });
      state.selectList = [];
    },
    //单选
    [CHECK_ITEM](state, index) {
      let id = state.list[index].id;
      let i = state.selectList.indexOf(id);
      //能在selectList找到对应的id,就删除
      if (i > -1) {
        return state.selectList.splice(i, 1);
      }
      //如果之前没有选中,就给selectList添加一个id进去
      state.selectList.push(id);
    },
    //删除
    delGoods(state) {
      state.list = state.list.filter((v) => {
        return state.selectList.indexOf(v.id) == -1;
      });
    },
  },
  actions: {
    checkAllFn({ commit, getters }) {
      getters.isCheckedAll ? commit("UN_CHECK_ALL") : commit("CHECK_ALL");
    },
    //删除商品操作
    delGoodsFn({ commit, state }, id) {
      if (state.selectList.length == 0) {
        Toast("请选择商品");
      }

      let arrCart = [];
      Dialog.confirm({
        message: "确定要删除这些商品吗？",
      })
        .then(() => {
          //单个删除
          if (typeof id == "number") {
            arrCart = [id];
            let index = state.list.findIndex((v) => {
              return (v.id = id);
            });
            state.list.splice(index, 1);
          } else {
            //多选删除
            arrCart = state.selectList;
            commit("delGoods");
          }

          http
            .$axios({
              url: "/api/deleteCart",
              method: "post",
              data: {
                arrId: arrCart,
              },
            })
            .then((res) => {
              Toast(res.msg);
            });
        })
        .catch(() => {
          // on cancel
        });
    },
  },
};
